package ink.lovejinhu.leetcode;

import java.util.HashSet;
import java.util.Iterator;

/**
 * @Author: jin
 * @Date: 2021/9/6 16:39
 * @Version: V1.0
 * @Slogan: Great people are not born great, but show their greatness in the process of growing up
 **/
public class Solution_0859 {
    /**
     * 竞赛中遇到过一次，交换一次影响两个字符
     * 交换一次就可以得到目标字符串
     *
     * @param s
     * @param goal
     * @return
     */
    public boolean buddyStrings(String s, String goal) {
        if (s.length() != goal.length()) {
            return false;
        }

        HashSet<Character> res = new HashSet<>();
        HashSet<Character> resgoalg = new HashSet<>();
        HashSet<Character> sSet = new HashSet<>();
        HashSet<Character> gSet = new HashSet<>();
        for (int i = 0; i < s.length(); i++) {
            sSet.add(s.charAt(i));
            gSet.add(goal.charAt(i));
            if (s.charAt(i) != goal.charAt(i)) {
                res.add(s.charAt(i));
                resgoalg.add(goal.charAt(i));
            }
        }
        if (res.size() == 0 && sSet.size() < s.length()) {
            return true;
        }

        if (res.size() == 2) {
            Iterator<Character> iterator = res.iterator();
            while (iterator.hasNext()) {
                if (!resgoalg.contains(iterator.next())) {
                    return false;
                }
            }
            return true;
        } else {
            return false;
        }

    }
}
